//
//  QKRecommandController.m
//  Gigle
//
//  Created by 丁乾坤 on 2016/9/28.
//  Copyright © 2016年 丁乾坤. All rights reserved.
//

#import "QKRecommandController.h"
#import "QKTopicCell.h"
#import "QKTopicModel.h"


@interface QKRecommandController ()

@property(nonatomic, strong)NSMutableArray *dataSource;

@end

@implementation QKRecommandController
//单元格表示符
static NSString *cellIdentifier = @"QKTopicCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    //注册单元格cell
    [self.tableView registerNib:[UINib nibWithNibName:@"QKTopicCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cellIdentifier];
    [self.tableView setContentInset:UIEdgeInsetsMake(0, 0, 64 + 49 + 38, 0)];
    
    //加载数据
    [self _loadNewData];
}


- (instancetype)initWithStyle:(UITableViewStyle)style{
    self = [super initWithStyle:UITableViewStylePlain];
    return self;
}

#pragma mark  数据请求
- (void)_loadNewData{
    NSString *url = @"http://s.budejie.com/topic/list/jingxuan/1/bs0315-iphone-4.5/0-20.json";
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *sessionDataTask = [session dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        NSDictionary *dictModel = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        QKTopicModel *topicModel = [QKTopicModel mj_objectWithKeyValues:dictModel];
        self.dataSource = topicModel.list;
        [self.tableView reloadData];
        
    }];
    [sessionDataTask resume];
    
}

#pragma mark 单元格数据源

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 150;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 15;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    QKTopicCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    
    if(!cell){
        cell = [[QKTopicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.topicModel = (ListContent *)self.dataSource[indexPath.section];
    
    return cell;
    
}


#pragma mark 代理

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}




@end
